package com.mall.controller;


import com.alibaba.fastjson.JSON;
import com.mall.entity.Product;
import com.mall.httpResult.HttpResult;
import com.mall.httpResult.ResultCodeEnum;
import com.mall.mapper.ProductMapper;
import com.mall.service.ProductService;
import com.mall.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.web.bind.annotation.*;
import sun.dc.pr.PRError;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author fire
 * @since 2020-04-16
 */
@RestController
@RequestMapping("/shoppingcar")
public class ShoppingcarController {

    @Autowired
    RedisUtil redisUtil;
    @Autowired
    HashOperations hashOperations;
    @Autowired
    ProductService productService;

    @PostMapping("/add/{userId}/{productId}")
    public HttpResult addShoppingCar(@PathVariable("userId") int userId, @PathVariable("productId") int productId, @RequestParam("num") int num) {
        try {
            Boolean hexists = hashOperations.hasKey("shoppingCar:" + userId + "", +productId + "");
            if (hexists) {
                String productJson = (String) hashOperations.get("shoppingCar:" + userId + "", +productId + "");
                Product product = JSON.parseObject(productJson, Product.class);
                product.setCount(product.getCount() + num);
                hashOperations.put("shoppingCar:" + userId + "", productId + "", JSON.toJSONString(product));
                return HttpResult.success();
            }
            Product product = productService.selectById(productId);
            product.setCount(num);
            hashOperations.put("shoppingCar:" + userId + "", productId + "", JSON.toJSONString(product));
            return HttpResult.success();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.failure(ResultCodeEnum.SERVER_ERROR);
    }

    @DeleteMapping("/delete/{userId}/{productId}")
    public HttpResult deleteCarItem(@PathVariable("userId") int userId, @PathVariable("productId") int productId) {
        hashOperations.delete("shoppingCar:" + userId + "", productId + "");
        return HttpResult.success();
    }

    @PostMapping("CarItem/{userId}/{productId}")
    public HttpResult updateCarItemNum(@PathVariable("userId") int userId, @PathVariable("productId") int productId, @RequestParam("num") int num) {
        boolean hexists = hashOperations.hasKey("shoppingCar:" + userId + "", +productId + "");
        if (!hexists) return HttpResult.failure(ResultCodeEnum.SHOPPINGCAR_ITEM_NOTEXISTS);
        Product product = JSON.parseObject((String) hashOperations.get("shoppingCar:" + userId + "", +productId + ""), Product.class);
        product.setCount(num);
        hashOperations.put("shoppingCar:" + userId + "", +productId + "", JSON.toJSONString(product));
        return HttpResult.success();
    }

    @GetMapping("/{userId}")
    public HttpResult getShoppingCar(@PathVariable("userId") int userId) {
        boolean exists = redisUtil.existsKey("shoppingCar:" + userId + "");
        if (!exists) return HttpResult.failure(ResultCodeEnum.SHOPPINGCAR_NOTEXISTS);
        Set<String> fields = hashOperations.keys("shoppingCar:" + userId + "");
        List<String> productsJSON = hashOperations.multiGet("shoppingCar:" + userId + "", fields);
        List<Product> products = new ArrayList<>();
        for (String productJSON : productsJSON) {
            products.add(JSON.parseObject(productJSON, Product.class));
        }
        return HttpResult.success(products);
    }

}

